Skip to content

Raise MessageParseError (not TypeError) on malformed field types#988

Open
Zeffut wants to merge 1 commit into
anthropics:mainfrom
Zeffut:fix/message-parser-rate-limit-typeerror
Open

Raise MessageParseError (not TypeError) on malformed field types#988
Zeffut wants to merge 1 commit into
anthropics:mainfrom
Zeffut:fix/message-parser-rate-limit-typeerror

Conversation

@Zeffut
Copy link
Copy Markdown

@Zeffut Zeffut commented May 23, 2026

Summary

parse_message in _internal/message_parser.py documents that "malformed input" should raise MessageParseError, but the six case branches only catch KeyError. A payload with a field of the wrong type (e.g. rate_limit_event with rate_limit_info=None, or any user/assistant/system/result/stream_event message where a sub-field is missing structure) leaks a bare TypeError. Because the parser is called inside the consumer's read loop, that escapes the loop and silently drops every subsequent message on the stream.

Change

  • Added a sibling except TypeError to each of the six case branches, raising MessageParseError("Malformed field in <type> message: ...", data=payload).
  • Kept the existing KeyError → "Missing required field..." messages verbatim so existing test assertions remain valid.
  • Added three regression tests in tests/test_message_parser.py covering rate_limit_info ∈ {None, str, int} and user/assistant message=None.

Verification

  • pytest tests/test_message_parser.py: 57 pass (54 prior + 3 new)
  • pytest tests/: 753 pass, 3 skipped
  • ruff check, ruff format --check, mypy src/: clean

…oads

parse_message documents that malformed messages raise MessageParseError, but
each case-branch's try/except only catches KeyError. When a field is present
but of the wrong type (e.g. rate_limit_event with rate_limit_info=None, or
user/assistant with message=None), the subsequent indexing raises TypeError,
which escapes the parser and crashes the read loop -- losing every subsequent
message in the stream.

Add a TypeError clause alongside the existing KeyError clause in the six case
branches that index sub-fields. Existing "Missing required field..." wording
is preserved for KeyError (backward-compatible with existing tests); the new
clause emits "Malformed field..." through the same MessageParseError type and
carries the original payload.

Co-Authored-By: Claude <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 23, 2026 21:32
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR hardens message parsing so malformed/legacy CLI payloads raise MessageParseError (with original data attached) instead of leaking TypeError and potentially crashing the parser loop.

Changes:

  • Added regression tests for malformed rate_limit_event payloads and non-dict message fields for user/assistant.
  • Updated parse_message to catch TypeError in multiple message-type branches and re-raise as MessageParseError.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
tests/test_message_parser.py Adds test coverage to ensure malformed payloads raise MessageParseError rather than TypeError.
src/claude_agent_sdk/_internal/message_parser.py Converts TypeError during parsing into MessageParseError across supported message types.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +253 to +256
except TypeError as e:
raise MessageParseError(
f"Malformed field in system message: {e}", data
) from e
Comment on lines +125 to +128
except TypeError as e:
raise MessageParseError(
f"Malformed field in user message: {e}", data
) from e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants